{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 奇偶数\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bf9b79b-3c16-4d38-b20a-3ed18c43f494",
   "metadata": {},
   "source": [
    "该代码通过用户输入一个数字来判断该数字的类型，具体来说，是判断它是否为整数以及是否为偶数或奇数。 \n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **用户输入**：代码首先让用户输入一个数字，并将其转换为浮点数。这种转换使得即便用户输入的是小数，也能正常处理。\n",
    "\n",
    "2. **整数判断**：代码使用`num.is_integer()`方法来检测该数字是否为整数。对于一个浮点数$x$，如果$x$是整数，则其满足$x = n$，其中$n$是整数。如果$x$包含小数部分，即$x = n + r$，其中$r \\neq 0$，则它被视为非整数。\n",
    "\n",
    "3. **偶数或奇数判断**：当输入的数字是整数时，代码进一步检查其是否为偶数或奇数。偶数定义为能够被$2$整除的数，即满足以下条件的数：\n",
    "\n",
    "   $$\n",
    "   x \\mod 2 = 0\n",
    "   $$\n",
    "\n",
    "   如果该条件成立，则输出数字是偶数；否则，该整数为奇数：\n",
    "\n",
    "   $$\n",
    "   x \\mod 2 = 1\n",
    "   $$\n",
    "\n",
    "4. **非整数处理**：如果输入的数字不是整数（即带有小数部分），代码直接输出该数字不是整数的信息。\n",
    "\n",
    "### 总结\n",
    "\n",
    "总体而言，代码通过判断用户输入的数字是否是整数以及它是否能被$2$整除来识别数字的类型。该逻辑确保无论用户输入何种数值，代码都能判断它是偶数、奇数还是非整数，给出相应的提示。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d3f36b4-3508-4e5c-9aad-9db8848df11a",
   "metadata": {},
   "source": [
    "## 判断一个数是否为偶数或奇数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a53bb21c-dee0-4be4-9e66-7acbc4772f8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Enter a number:  6\n"
     ]
    }
   ],
   "source": [
    "num = float(input(\"Enter a number: \"))  # 输入一个数字并转换为浮点数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b9a07ce-82fd-4157-83e3-d346d8530fe3",
   "metadata": {},
   "source": [
    "## 检查输入的数字是否为整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f5daa794-1cc9-4336-b18b-57ff3c34fbe3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 is even \n"
     ]
    }
   ],
   "source": [
    "if num.is_integer():  # 如果数字为整数\n",
    "\n",
    "    ## 判断整数是偶数还是奇数\n",
    "    if (num % 2) == 0:  # 如果数字除以2的余数为0，则为偶数\n",
    "       print(\"{0} is even \".format(int(num)))  # 输出偶数信息\n",
    "    else:  # 否则为奇数\n",
    "       print(\"{0} is odd \".format(int(num)))  # 输出奇数信息\n",
    "\n",
    "else:  # 如果数字不是整数\n",
    "    print(\"{0} is not an integer \".format(num))  # 输出非整数信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
